/*
 *  particle.cpp
 *  mograph
 *
 *  Created by parktvadmin on 11/3/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "particle.h"

Particle::Particle(ofPoint l){
	counter = 0;
	float randmin = -HALF_PI;
	float randmax = 0;
	
	float r = ofRandom(0, TWO_PI);
	float x = cos(r);
	float y = sin(r);
	acc = ofPoint(x / 250, y / 250);
	
	float q = ofRandom(0, 1);
	r = ofRandom(randmin, randmax);
	x = cos(r) * q;
	y = sin(r) * q;
	vel = ofPoint(x, y);
	loc = l;
	//hist = new ofPoint[1000];
}

void Particle::update(){
	vel += acc;
	loc += vel;
	//if(ofGetFrameNum() % 10 == 0 && counter < 1000){
	//	hist[counter].x = loc.x;
	//	hist[counter].y = loc.y;
	//	counter++;
	//}
}

void Particle::draw(){
	ofFill();
	ofSetColor(100, 100, 100, 100);
	//drawArrowHead(vel, loc, 10);
	ofFill();
	ofSetColor(0, 0, 0, 100);
	ofBeginShape();
	//for(int i = 0; i < counter; i++){
	//	ofVertex(hist[i].x, hist[i].y);
	//}
	//if(counter > 0) ofVertex(loc.x, loc.y);
	ofEllipse(loc.x, loc.y, 5, 5);
	ofEndShape(false);
}

void Particle::drawArrowHead(ofPoint v, ofPoint loc, float scale){
	ofPushMatrix();
	float arrowsize = 5.0f;
	ofTranslate(loc.x, loc.y, 0);
	float rotate = atan2(v.y, v.x);
	ofRotate(ofRadToDeg(rotate), 0, 0, 1);
	
	float len = mag(v) * scale;
	arrowsize = ofMap(len, 0.f, 10.f, 0.f, 1.f, false) * arrowsize;
	
	ofLine(0, 0, len-arrowsize, 0);
	
	ofBeginShape();
	ofVertex(len, 0);
	ofVertex(len-arrowsize, arrowsize/2);
	ofVertex(len-arrowsize, -arrowsize/2);
	ofEndShape(true);
	
	ofPopMatrix();
}

float Particle::mag(ofPoint in){
	float retf = sqrt(in.x * in.x + in.y * in.y);
	return retf;
}